package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ChooseClassData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerCreateClassComponent;
import com.eduhzy.ttw.teacher.mvp.contract.CreateClassContract;
import com.eduhzy.ttw.teacher.mvp.presenter.CreateClassPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_CHOOSECLASSACTIVITY)
public class ChooseClassActivity extends BaseActivity<CreateClassPresenter> implements CreateClassContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @Inject
    BaseQuickAdapter<ChooseClassData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    List<ChooseClassData> lists;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerCreateClassComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_choose_class; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.getZxxx02ListByXxid();
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        swipeLayout.setOnRefreshListener(() ->
                //此方法名字根据接口文档定
                mPresenter.getZxxx02ListByXxid()
        );
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ChooseClassData chooseClassData = lists.get(position);
            chooseClassData.setSelected(!chooseClassData.isSelected());
            mAdapter.notifyDataSetChanged();
            Message message = Message.obtain();
            message.what = EventBusTags.CHOOSE_CLASS_COMPLETE;
            message.obj = chooseClassData;
            EventBus.getDefault().post(message,EventBusTags.CREATE_CLASS);
            finish();
        });
    }

    @Override
    public void showLoading() {
        swipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
//        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Context getActivity() {
        return this;
    }
}
